$(function (){
    var isClick = false;
    var cache = {};
    function ajaxLoad(index, $content, $labels) {
        var url;
        var i, len;
        var ajaxs = [];
        if (index === 0) {
            url = "/ICBCDynamicSite2/money/services/MoneyListServiceN.ashx";
        } else if (index === 2) {
            url = "/ICBCDynamicSite/services/GetFundInfoN.ashx";
        }
        for (i = 0, len = $labels.length; i < len; i++) {
            (function (i) {
                var objId = $labels[i].getAttribute("data-obj-id");
                ajaxs.push($.ajax({
                    type: "POST",
                    url: url,
                    dataType: "json",
                    data: {
                        keyword_objID: objId
                    }
                }));
            })(i);
        }
        $.when.apply(window, ajaxs)
        .done(function () {
            var field;
            var results = [];
            var data, unitData;
            if (arguments.length === 3 && typeof arguments[1] === "string") {
                results.push(arguments);
            } else {
                results = arguments;
            }
            for (i = 0, len = results.length; i < len; i++) {
                field = $labels[i].getAttribute("data-field");
                hide = $labels[i].getAttribute("data-hide");
                data = results[i][0];
                if (data.pub.retCode === "0" && results[i][0].pvt.length && !hide) {
                    unitData = results[i][0].pvt[0] || {};
                    $labels.eq(i).parent().html(unitData[field]);
                } else {
                    if (window.console && data.pub) console.log(data.pub.retMsg);
                }
            }
            // if (!flag && $labels.length > 0 && cache[index] < 5) setTimeout(function () {
            //     cache[index] += 1;
            //     dataLoad(index, $content);
            // }, 3000);
        })
        .fail(function (results) {
            var a = 1;
            a = 2;
        });
    }
    function dataLoad(index, $content) {
        var $labels;
        if (index === 0) {
            if (!cache[index]) {
                cache[index] = 1;
                $labels = $content.find("label.finance_sdc");
                if ($labels.length) ajaxLoad(index, $content, $labels);
            }
        } else if (index === 1) {
            if (!cache[index]) {
                cache[index] = 1;
                RereshData();
            }
        } else if (index === 2) {
            if (!cache[index]) {
                cache[index] = 1;
                $labels = $content.find("label.fund_sdc");
                ajaxLoad(index, $content, $labels);
            }
        } else if (index === 3) {
            if (!cache[index]) {
                cache[index] = 1;
                GetRefresh("Q2Table");
                GetRefresh("Q3Table");
            }
        }
    }
    function switchTab(jqId, subJqId, callback) {
        var $tab = $(jqId);
        var $contents = $(subJqId + " > div");
        var time;
        $tab.on("mouseover", "li", function () {
            var $li = $(this);
            if ($li.hasClass("on")) return false;
            time = setTimeout(function () {
                $li.addClass("on").siblings().removeClass("on");
                var index = $li.index();
                var $content = $contents.eq(index);
                $content.show().siblings().hide();
                if (callback) callback(index, $content);
            }, 200);
        });
        $tab.on("mouseout", "li", function () {
            clearTimeout(time);
        });
    }
    // 投资理财
    switchTab("#tab_t", "#tab_c", dataLoad);
    // 便利金融
    switchTab("#tab_tt", "#tab_ct");
    // 初始加载理财数据
    dataLoad(0, $("#tab_c > div").eq(0));

    $(".tzlc_footer .r li").hover(function(){
        if($(this).hasClass("on")){
            $(this).removeClass("on")
        }else {
            $(this).addClass("on")
        }
    });
    $(".tzlc_title .roata_update").click(function(){
        if(isClick) return false;
        isClick = true;
        var _this = $(this)
        _this.addClass("on");
        setTimeout(function(){
            _this.find('.succes_updata').fadeIn(200).delay(1500).fadeOut(200,function(){
                isClick = false;
            });
        },2000);
    });
    function show(){
        $(".tzlc_title  .roata_update").removeClass("on")
    }
    setInterval(show,2000);
    $("#selectTBox li,#tab_ct ul li").hover(function(){
        if($(this).hasClass("on")){
            $(this).removeClass("on")
        }else {
            $(this).addClass("on")
        }
    });
    //优惠生活
    $("#action_Y_r li,.action_Y_more .l li").hover(function(){
        if($(this).hasClass("on")){
            $(this).removeClass("on");
            $(" .kv_more").removeClass("on")
        }else {
            $(this).addClass("on");
            $(" .kv_more").addClass("on")
        }
    });
    //工行风貌
    $("#texttBox dd ,#texttBox dd span").hover(function(){
        if($(this).hasClass("on")){
            $(this).removeClass("on")
        }else {
            $(this).addClass("on")
        }
    });
    $(".action_M .r .action_M_other td").hover(function(){
        var _this =$(this);
        if(_this.hasClass("on")){
            _this.removeClass("on")
        }else {
            _this.addClass("on");
        }
    });
});
